<template>
  <div class="balance-area" v-show="visiable" v-click-outside="close">
    <Loading v-show="loading" />
    <div v-show="!loading">
      <div class="txt">
        <div>{{ sender }}</div>
        <div>ID:{{ showId }}</div>
      </div>
      <div class="centerners">
        <div class="money">余额：{{ balance }}</div>
        <div>已冻结：{{ freeze }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { getOtherUserBalance } from '@/api'
import { Loading } from 'vant'

type Props = {
  senderId: string
  sender: string
}
const showId = ref('')

const props = defineProps<Props>()

const loading = ref(false)
const visiable = ref(false)
const balance = ref('')
const freeze = ref('')

const formatNum = (num) => {
  if (!num) return '0'
  const reg = /(\d)(?=(?:\d{3})+$)/g
  return num.toString().replace(reg, '$1,')
}

const close = () => {
  visiable.value = false
}

const open = async () => {
  if (visiable.value) {
    visiable.value = false
    return
  }
  loading.value = true
  visiable.value = true
  try {
    const { data } = await getOtherUserBalance({
      id: props.senderId,
    })
    balance.value = formatNum(data.balance)
    freeze.value = formatNum(data.freeze)
    showId.value = data.showId
  } catch {
    /* empty */
  }
  loading.value = false
}

defineExpose({
  open,
})
</script>

<style lang="less" scoped>
.balance-area {
  position: absolute;
  width: max-content;
  right: -12px;
  top: 50%;
  padding: 9px 7px;
  transform: translate(100%, -50%);
  background-color: rgba(49, 49, 49, 1);
  box-shadow: 0px 0px 4px 0px rgba(36, 5, 5, 0.25);
  border-radius: 8px;
  z-index: 2;
  text-align: left;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translate(-100%, -50%);
    width: 0;
    height: 0;
    border: 5px solid transparent;
    border-right: 10px solid rgb(49, 49, 49);
  }

  .txt {
    padding-left: 6px;
  }
}

.centerners {
  margin-top: 2px;
  background: rgba(68, 80, 137, 1);
  border-radius: 4px;
  padding: 4px 6px;

  .money {
    color: #ffff;
    font-size: 11px;
    font-family: 'ShiShangZhongHei';
    padding-right: 2px;
    span {
      font-size: 10px;
    }
  }

  .cnter {
    padding: 2px 2px;
    background: #7582df;
    border-radius: 15px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }
}
</style>
